
$(function(){
	checkForm();
	$('.addLink').click(addContact);
})

function checkForm(){
	$('form').eq(1).bootstrapValidator({
		  fields: {
			  trigger:'blur change',
			  fullname: {
				  trigger:'blur change',
		          validators: {
		              notEmpty: {
		                  message: '客户名称为空'
		              },
		              callback:{
		            	  message: '客户已存在',
		            	  callback: function(value, validator){
		            			if(!value) return true;
		            			var unused=true;
		            			var data={fullname:value.trim()};
		            			var id=$('#id').html();
		            			if(id) data.id=id;;
		            			$.ajax({
		            				url:'../customer/doCheckFullname',
		            				type:'get',
		            				data:data,
		            				dataType:'json',
		            				async:false,
		            				success:function(result){
		            					if(result.head.respCode=='0000000'){
		            						if(result.body){
		            							unused=false;
		            						}
		            					}
		            				}
		            			});
		            			return unused;
		            		}
		              }
		          }
		      },
		      abbreviation: {
		    	  trigger:'blur',
		          validators: {
		        	  callback: {
		                  message: '客户简称已存在',
		                  callback: function(value, validator){
		                		if(!value) return true;
		                		var unused=true;
		                		var data={abbreviation:value.trim()};
		                		var id=$('#id').html();
		                		if(id) data.id=id;
		                		if(abbreviation){
		                			$.ajax({
		                				url:'doCheckAbbreviation',
		                				type:'get',
		                				data:data,
		                				dataType:'json',
		                				async:false,
		                				success:function(result){
		                					if(result.head.respCode=='0000000'){
		                						if(result.body){
		                							unused=false;
		                						}
		                					}
		                				}
		                			});
		                		}
		                		return unused;
		                	}
		              }
		          }
		      },
		      linkman: {
		    	  trigger:'blur',
		          validators: {
		        	  notEmpty: {
		        		  message: '联系人为空',
		              }
		          }
		      },
		      contact: {
		    	  trigger:'blur',
		          validators: {
		        	  notEmpty: {
		        		  message: '联系方式为空',
		              },
		              regexp: {
	                        regexp: /^(0\d{2,3}-?\d{7,8})|(1\d{10})$/,
	                        message: '联系方式不符'
	                  }
		          }
		      },
		      email: {
		    	  trigger:'blur',
		    	  validators: {
		    		  regexp: {
	                        regexp: /^\w+@\w+(.[a-zA-Z0-9_-])+$/,
	                        message: '邮箱格式不符'
	                  }
		          }
		      },
		      updateCount:{
		    	  trigger:'blur',
		    	  validators: {
		              regexp: {
	                        regexp: /^\d*$/,
	                        message: '必须是数字'
	                  }
		          }
		      }
		  }
	});
}

function addContact(){
	var contact=$('<tr></tr>');
	var td1=$('<td><table class="table"><tr><td style="border:none; padding: 8px 0" width="auto">'
			+'<span><label class="star">*</label>姓名：</span></td>	<td  style="border:none"><div class="form-group">'
            +'<input type="text" class="form-control" name="linkman" /></div></td></tr></table></td>');
	
	var td2=$('<td><table class="table"><tr><td  style="border:none;padding: 8px 0" width="auto">'
			+'<span><label class="star">*</label>手机号：</span></td>	<td  style="border:none"><div class="form-group">'
            +'<input type="text" class="form-control" name="contact" /></div></td></tr></table></td>');
	
	var td3=$('<td class="find_div"><table class="table"><tr><td  style="border:none;padding: 8px 0"width="auto">'
			+'<span>邮箱：</span></td>	<td  style="border:none"><div class="form-group">'
			+'<input type="text" class="form-control"" name="email" /></div></td></tr></table>'
			+'<div class="find_div_xianshi" ></div></td>');
	td3.find('input').keyup(promptEmail).blur(bindClick);
	var td4=$('<td><table class="table"><tr><td  style="border:none;padding: 8px 0"width="auto">'
			+'<span>职位/部门：</span></td>	<td  style="border:none"><div class="form-group">'
            +'<input type="text" class="form-control" name="deptOrPos" /></div></td></tr></table></td>');
	var td5=$('<td style="margin-left:20px"><button type="button" class="btn btn-default btn-md btn-flat">删除</button></td>');
	contact.append(td1).append(td2).append(td3).append(td4);
	td5.children().click(removeContact);
	contact.append(td5);
	$('.contacts').append(contact);
	//解除表单验证，再添加
	$('form').eq(1).data('bootstrapValidator').destroy();
	$('form').eq(1).data('bootstrapValidator', null);
	checkForm();
}


